﻿using Aop.Api.Domain;
using BuyWithoutLoseMoney;
using BuyWithoutLoseMoney.Core.Models;
using BuyWithoutLoseMoney.Core.Repositories;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using System.Linq;
using System.Threading.Tasks;

namespace BuyWithoutLose.Areas.Admin.Controllers
{
    /// <summary>
    /// 订单
    /// </summary>
    [Area("admin")]
    public class OrderController : BaseAdminController
    {
        private readonly IRepository<Order,int> _orderRepository;
        private readonly IRepository<User,int> _userRepository;
        public OrderController( IRepository<Order,int> orderRepository,IRepository<User,int> userRepository, BuyWithoutLoseMoneyDBContext db) : base(db)
        {
            _orderRepository = orderRepository;
            _userRepository = userRepository;
        }
        #region 订单的列表展示，查询，修改，删除
        /// <summary>
        /// 订单列表展示
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> Index()
        {
            var order=await _orderRepository.GetAllListAsync();
            ViewBag.order = order;
            var user = await _userRepository.GetAllListAsync();
            ViewBag.User=user;
            return View(order);
        }
        /// <summary>
        /// 订单查询
        /// </summary>订单编号，用户，状态
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Index(int page,int limit,int orderid=0,int userid=0,int statusId=0)
        {
            var order = _orderRepository.GetAll();
            var query = order.Include("User").Where(x => true);
            //搜索订单编号
            if (orderid != 0)
            {
                query=query.Where(x=>x.Id==orderid);
            }
            //搜索用户
            if (userid != 0) 
            { 
                query=query.Where(x=>x.UserId==userid);
            }
            //搜索状态
            if (statusId != 0)
            {
                query = query.Where(x => x.Status == statusId);
            }
            var count=query.Count();
            var list = query.OrderBy(x => x.Id).Skip((page - 1) * limit).Take(limit).ToList()
              .Select(x => new
              {
                  id = x.Id,
                  userName = x.User.NickName,
                   totalmoney=x.TotalMoney,
                  status = x.Status,
                  receivingtype= x.ReceivingType,
                  //receivingaddress=x.ReceivingAddress,
                  createtime=x.CreateTime.ToString(),
                  arrivetime=x.ArriveTime.ToString(),
                  paymethod=x.Paymethod
              });
            return Json(new { Code = 0, Msg = "", Data = list, Count = count });
        }
        /// <summary>
        /// 添加订单
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> Create()
        {
            var order=await _orderRepository.GetAllListAsync();
            ViewBag.Order=order;
            var user = await _userRepository.GetAllListAsync();
            ViewBag.User=user;
            return View();
        }
        [HttpPost]
        public async Task<IActionResult> Create(Order order)
        {
            if (ModelState.IsValid)
            {
                await _orderRepository.InsertAsync(order);
                return Json(new { isSuccess = true, msg = "添加成功，页面即将刷新！" });
            }
            return Json(new { isSuccess = false, msg = "添加失败，请重试！" }) ;
        }
        /// <summary>
        /// 修改订单
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> Edit(int id)
        {
            var order = await _orderRepository.GetAllListAsync();
            var o=order.FirstOrDefault(x=>x.Id==id);
            var user=await _userRepository.GetAllListAsync();
            ViewBag.User = user;
            return View(o);
        }
        [HttpPost]
        public async Task<IActionResult> Edit(Order order)
        {
            if (ModelState.IsValid)
            {
                await _orderRepository.UpdateAsync(order);
                return Json(new { isSuccess = true, msg = "修改成功，页面即将刷新！" });
            }
            return Json(new { isSuccess = false, msg = "修改失败，请重试！" });
        }
     /// <summary>
     /// 删除订单
     /// </summary>
     /// <returns></returns>
        public async Task<IActionResult> Del(int id)
        {
            Order o=new Order() { Id=id};
            if(ModelState.IsValid)
            {
                await _orderRepository.DeleteAsync(o);
                return Json(new { isSuccess = true, msg = "删除成功，页面即将刷新！" });
            }
            return Json(new { isSuccess = false, msg = "修改失败，请重试！" });
        }
        #endregion
    }
}
